package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.LpojosVo.LxsmbVo;
import cn.gson.fengboot.model.dao.DdepDao;
import cn.gson.fengboot.model.dao.DempDao;
import cn.gson.fengboot.model.dao.LxsmbDao;
import cn.gson.fengboot.model.dao.LxsmbcbDao;
import cn.gson.fengboot.model.mapper.LxsmbMapper;
import cn.gson.fengboot.model.pojos.Lxsbm;
import cn.gson.fengboot.model.pojos.Lxsmbcb;
import cn.gson.fengboot.model.pojos.QdepFrom;
import cn.gson.fengboot.model.pojos.QempFrom;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor =  Exception.class)
public class LxsmbService {
    @Autowired
    LxsmbDao lxsmbDao;
    @Autowired
    LxsmbcbDao lxsmbcbDao;
    @Autowired
    LxsmbMapper lxsmbMapper;
    @Autowired
    DdepDao ddepDao;
    @Autowired
    DempDao dempDao;

    //查询部门目标总值
    public List<Lxsbm> bumenmubiao(Integer deptid,String shijian){
        return lxsmbMapper.bumenmubiao(deptid,shijian);
    }

    public Map<String,Object> ygyjselect(String date3){
//        System.out.println("www"+date3);
        Map<String,Object> map = new HashMap<>();
        List<LxsmbVo> ygyuyeji = lxsmbMapper.ygyjselect(date3);//员工月业绩查询
        List<LxsmbVo> ygyemb = lxsmbMapper.ygmbselect(date3);//员工月目标查询
//        System.out.println("www"+ygyuyeji);
//        System.out.println("www"+ygyemb);
        List<String> ygname = new ArrayList<>();//员工姓名
        List<BigDecimal> yghkyeji = new ArrayList<>();//员工回款业绩
        List<BigDecimal> ygqyyeji = new ArrayList<>();//员工签约业绩
        List<BigDecimal> ygyhkmb= new ArrayList<>();//员工回款目标
        List<BigDecimal> ygyqymb= new ArrayList<>();//员工签约目标
        for (int i =0;i<ygyuyeji.size();i++){
            for(int j =0;j<ygyuyeji.size();j++){
                if(ygyuyeji.get(i).getYgname().equals(ygyemb.get(j).getYgname())){
                    ygname.add(ygyuyeji.get(i).getYgname());
                    System.out.println("1111"+ygyuyeji.get(i).getHkmoeny());
                    yghkyeji.add(ygyuyeji.get(i).getHkmoeny());
                    ygqyyeji.add(ygyuyeji.get(i).getSummoeny());
                    ygyhkmb.add(ygyemb.get(j).getHkmoeny());
                    ygyqymb.add(ygyemb.get(j).getSummoeny());
                }
            }
        }
        map.put("ygname",ygname);
        map.put("yghkyeji",yghkyeji);
        map.put("ygqyyeji",ygqyyeji);
        map.put("ygyhkmb",ygyhkmb);
        map.put("ygyqymb",ygyqymb);
        return map;
    }

    public Map<String,Object> yearselect(String date1,String date2){
        Map<String,Object> map = new HashMap<>();
        List<LxsmbVo> lxsmbVos =lxsmbMapper.yearselect(date1,date2);
        List<BigDecimal> ymoney = new ArrayList<>();
        List<BigDecimal> hkmoney = new ArrayList<>();
        List<Double> gshk = new ArrayList<>();//公司回款总目标
        List<Double> gsqy = new ArrayList<>();//公司回款总目标
        List<Lxsbm> mbz = lxsmbMapper.gsmbselect(date1,date2);//总目标
        List<String> yearrd = new ArrayList<>();
        for (int i = 0; i<lxsmbVos.size();i++){
            ymoney.add(lxsmbVos.get(i).getSummoeny());
            hkmoney.add(lxsmbVos.get(i).getHkmoeny());
            yearrd.add(lxsmbVos.get(i).getYear());
            gshk.add(mbz.get(i).getXsmbHuikuangje());
            gsqy.add(mbz.get(i).getXsmbQianyuesl());
        }
        map.put("ymoney",ymoney);
        map.put("hkmoney",hkmoney);
        map.put("yearrd",yearrd);
        map.put("gshk",gshk);
        map.put("gsqy",gsqy);
//        System.out.println(map);
        return map;
    }

    //查询部门
    public Map<String,Object> depselect(Integer deptid){
        Map<String,Object> map = new HashMap<>();
        List<QdepFrom> qdepFrom = lxsmbMapper.depselect(deptid);
        map.put("qdepFrom",qdepFrom);
        return map;
    }




    //新增
    @Transactional(transactionManager = "transactionManager")
    public  void AddXsmb(String fun){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            System.out.println(fun);
            JSONObject jsonObject = JSON.parseObject(fun);
            JSONObject addBmmb = (JSONObject)jsonObject.get("AddBmmb");//主从表对象
            Integer xsmbId = (Integer)addBmmb.get("xsmbId");
            JSONArray qdepFrom = (JSONArray)addBmmb.get("qdepFrom");//部门id数组
            String xsmbQianyuesl = (String)addBmmb.get("xsmbQianyuesl");//部门签约金额目标
            String xsmbHuikuangje = (String)addBmmb.get("xsmbHuikuangje");//部门回款金额目标
            String xsmbZddate = (String)addBmmb.get("xsmbZddate");//部门创建目标日期
            Date date=sdf.parse(xsmbZddate);//主表时间格式
            JSONObject lxsmbcbs = (JSONObject)addBmmb.get("lxsmbcbs");//从表对象
            JSONArray qempFrom = (JSONArray)lxsmbcbs.get("qempFrom");//员工id数组
            Integer xsbmcbId = (Integer)lxsmbcbs.get("xsbmcbId");//从表id
            String xsmbcbDate = (String)lxsmbcbs.get("xsmbcbDate");
            Date date1=sdf.parse(xsmbcbDate);//从表表时间格式
            String xsmbcbHuikuangje = (String)lxsmbcbs.get("xsmbcbHuikuangje");//员工回款金额目标
            String xsmbcbQianyuesl = (String)lxsmbcbs.get("xsmbcbQianyuesl");//员工签约金额目标
            List<Object> ygid =new LinkedList<>();
            System.err.println("长度"+qempFrom.size());//
            Integer c =Integer.valueOf(qdepFrom.get(0)+"");
            for (int i = 0;i<qdepFrom.size();i++){
                if(c<Integer.valueOf(qdepFrom.get(i)+"")){
                    c= Integer.valueOf(qdepFrom.get(i)+"");
                }
            }
            QdepFrom bmdx = ddepDao.findByDepId(c);//部门对象

            //新增主表
            Lxsbm lxsbm = new Lxsbm();
            lxsbm.setXsmbId(xsmbId);
            lxsbm.setXsmbHuikuangje(Double.valueOf(xsmbHuikuangje));
            lxsbm.setXsmbQianyuesl(Double.valueOf(xsmbQianyuesl));
            lxsbm.setQdepFrom(bmdx);
            lxsbm.setXsmbZddate(date);
            Lxsbm save = lxsmbDao.save(lxsbm);

            List<QempFrom> qempFroms = new LinkedList<>();//员工集合
            for (int i = 0;i<qempFrom.size();i++){
                qempFroms.add(dempDao.findById(Integer.valueOf(qempFrom.get(i)+"")).get());
            }

            for (int i = 0;i<qempFroms.size();i++){
                Lxsmbcb lxsmbcb = new Lxsmbcb();
                lxsmbcb.setXsbmcbId(xsbmcbId);
                lxsmbcb.setXsmbcbDate(date1);
                lxsmbcb.setXsmbcbHuikuangje(Double.valueOf(xsmbcbHuikuangje));
                lxsmbcb.setXsmbcbQianyuesl(Double.valueOf(xsmbcbQianyuesl));
                lxsmbcb.setLxsbm(save);
                lxsmbcb.setQempFrom(qempFroms.get(i));
                lxsmbcbDao.save(lxsmbcb);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    //查询部门
    public Map<String,Object> empselect(Integer deptid){
        Map<String,Object> map = new HashMap<>();
        List<QdepFrom> qdepFrom = lxsmbMapper.empselect(deptid);
        map.put("qdepFrom",qdepFrom);
        return map;
    }



}
